#include "ball.h"
#include "CSurface.h"

Ball::Ball()
{
	location = Point(360,300);
	speed = Speed(location, 0, 0);
	filename = "ball.bmp";
	surf = CSurface::OnLoad(filename);
	width = 10;
	height = 10;
	speed.xSpeed = 1;
	speed.ySpeed = 1;
}

void Ball::draw(SDL_Surface *display)
{
	CSurface::OnDraw(display, surf, location.x, location.y, 0, 0, width, height);
}

void Ball::update(Paddle *p1, Paddle *p2)
{
	location.x += speed.xSpeed;
	location.y += speed.ySpeed;

	//(location.x <= 0) ? p2->score++ : p1->score++;

	if (location.x <= 0) 
	{
		p2->score++;
		location = Point(200 + p2->score*10, 360);
	}
	else if( location.x >= X_MAX - width)
	{
		p1->score++;
		location = Point(200 - p1->score*10, 360);
	}
}

void Ball::update(int xDir, int yDir)
{
	speed.xSpeed *= xDir;
	speed.ySpeed *= yDir;
}

bool Ball::checkCollision(Paddle *p)
{
	if ((p->location.y < location.y) && (p->location.y + p->height > location.y))
		this->update(-1, 1);	// change x-direction

	return false ;
}